import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.read_excel('sum_time.xlsx', header=None, names=['clade', 'normal_crown', 'normal_stem', 'skew_normal_crown', 'skew_normal_stem', 'uniform_crown', 'uniform_stem'])
# 设置英文和数字的字体为 Times New Roman
plt.rcParams['font.family'] = 'Times New Roman'
# 数据清洗函数
def parse_time_interval(interval_str):
    try:
        if '-' in interval_str:
            parts = interval_str.split('-')
            if len(parts) == 2:
                return [float(parts[0]), float(parts[1])]
            else:
                return None
        else:
            return None
    except ValueError:
        return None
# 应用数据清洗与提取
df['normal_crown'] = df['normal_crown'].apply(parse_time_interval)
df['normal_stem'] = df['normal_stem'].apply(parse_time_interval)
df['skew_normal_crown'] = df['skew_normal_crown'].apply(parse_time_interval)
df['skew_normal_stem'] = df['skew_normal_stem'].apply(parse_time_interval)
df['uniform_crown'] = df['uniform_crown'].apply(parse_time_interval)
df['uniform_stem'] = df['uniform_stem'].apply(parse_time_interval)
df = df.dropna(subset=['normal_crown', 'normal_stem', 'skew_normal_crown', 'skew_normal_stem', 'uniform_crown', 'uniform_stem'], how='all')
clades = df['clade'].tolist()
normal_crown = df['normal_crown'].tolist()
normal_stem = df['normal_stem'].tolist()
skew_normal_crown = df['skew_normal_crown'].tolist()
skew_normal_stem = df['skew_normal_stem'].tolist()
uniform_crown = df['uniform_crown'].tolist()
uniform_stem = df['uniform_stem'].tolist()
# 计算中值
normal_crown_mid = [np.mean(i) for i in normal_crown if i is not None]
normal_stem_mid = [np.mean(i) for i in normal_stem if i is not None]
skew_normal_crown_mid = [np.mean(i) for i in skew_normal_crown if i is not None]
skew_normal_stem_mid = [np.mean(i) for i in skew_normal_stem if i is not None]
uniform_crown_mid = [np.mean(i) for i in uniform_crown if i is not None]
uniform_stem_mid = [np.mean(i) for i in uniform_stem if i is not None]
# 确保所有列表长度一致
min_length = min(len(normal_crown_mid), len(skew_normal_crown_mid), len(uniform_crown_mid))
normal_crown_mid = normal_crown_mid[:min_length]
skew_normal_crown_mid = skew_normal_crown_mid[:min_length]
uniform_crown_mid = uniform_crown_mid[:min_length]
clades = clades[:min_length]
# 绘制图表
fig, ax = plt.subplots(figsize=(12, 8))
x = np.arange(len(clades))
width = 0.2
ax.bar(x - width*1.5, normal_crown_mid, width, label='Normal Crown', color='skyblue')
ax.bar(x - width*0.5, skew_normal_crown_mid, width, label='Skew-Normal Crown', color='lightgreen')
ax.bar(x + width*0.5, uniform_crown_mid, width, label='Uniform Crown', color='lightcoral')
ax.set_xlabel('Clade', fontsize=12)
ax.set_ylabel('Time Interval Midpoint', fontsize=12)
ax.set_title('Comparison of Time Interval Midpoints for Crown Groups', fontsize=14)
ax.set_xticks(x)
ax.set_xticklabels(clades, rotation=90, fontsize=15)
ax.legend()
plt.tight_layout()
plt.show()
# 绘制箱线图
data = [normal_crown_mid, skew_normal_crown_mid, uniform_crown_mid]
fig, ax = plt.subplots(figsize=(10, 6))
ax.boxplot(data, labels=['Normal', 'Skew-Normal', 'Uniform'], patch_artist=True,
           boxprops=dict(facecolor='skyblue', color='black'),
           whiskerprops=dict(color='black'),
           capprops=dict(color='black'),
           medianprops=dict(color='red'))
ax.set_title('Distribution of Time Interval Midpoints for Crown Groups', fontsize=14)
ax.set_ylabel('Time Interval Midpoint', fontsize=12)
plt.grid(True, linestyle='--', alpha=0.7)
plt.tight_layout()
plt.show()